﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ServiceCreated : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int serviceId = 0;
        try
        {

            ProxyWrapper.UserProxy.UserDetails _objUser = ((ProxyWrapper.UserProxy.UserDetails)Session["UserObject"]);
            ProxyWrapper.ServicesServiceWrapper _Service = null;// new ProxyWrapper.ServicesServiceWrapper();
            string serviceType = Request["servicetype"].ToString();
            string serviceSubType = Request["servicesubtype"].ToString();
            string[] msg = new string[2];//_Service.CreateService(serviceType, serviceSubType,);
            switch ((Enums.ServiceType)Enum.Parse(typeof(Enums.ServiceType), serviceType))
            {
                case Enums.ServiceType.record:
                    _Service = new ProxyWrapper.ServicesServiceWrapper();
                    string _detail = "Scans will be saved in the cloud. If a device loses its internet connection, scans can be saved on the device and uploaded later.";
                    string _serviceName = "Record Online Service";
                    string _serviceType = "Record Scans Online";
                    string _image = "Images/record_online.png";
                    string _serviceURL = "http://ssica.org/services/Services.asmx?op=ScanDataOnline";
                    if (serviceSubType == "ondevicerecord")
                    {
                        _image = "Images/record_offline.png";
                        _serviceType = "Record Scans On-Device";
                        _serviceName = "Record On-Device Service";
                        _detail = "Scans will be saved on the device. Users can upload their scans later at their discretion.";
                        _serviceURL = "http://ssica.org/services/Services.asmx?op=SyncDataFromDevice";
                    }
                    msg = _Service.CreateRecordScansService(_serviceName, _serviceType, serviceSubType, _objUser.ClientId, _objUser.HashCode, "", _detail, _image, _serviceURL);
                    break;
                case Enums.ServiceType.webview:
                    break;
                case Enums.ServiceType.databasebuilder:
                    break;
                case Enums.ServiceType.postBack:
                    break;
                case Enums.ServiceType.database:
                    string dbId = Request["database"] != null ? Request["database"] : "0";
                    _Service = new ProxyWrapper.ServicesServiceWrapper();
                    string _databaseDetail = "Scans validate against our servers.";
                    string _databaseServiceName = "Validate Online Service";
                    string _databaseServiceType = "Validate Scans Online";
                    string _databaseImage = "Images/validate_online.png";
                    string _databaseServiceURL = "http://ssica.org/services/Services.asmx?op=ValidateOnlineScans";
                    if (serviceSubType == "ondevicedatabase")
                    {
                        _databaseImage = "Images/validate_offline.png";
                        _databaseServiceType = "Validate Scans On-Device";
                        _databaseServiceName = "Validate On-Device Service";
                        _detail = "Your database is downloaded to the device before scanning and validated locally. Scan results are uploaded later at your discretion.";
                        _databaseServiceURL = "http://ssica.org/services/Services.asmx?op=ValidateOfflineScans";
                    }
                    ProxyWrapper.CommonProxyService.ValidateScan _scan = new ProxyWrapper.CommonProxyService.ValidateScan();
                    _scan.serviceName = _databaseServiceName;
                    _scan.serviceSubType = serviceSubType;
                    _scan.serviceDetail = _databaseDetail;
                    _scan.addValueWhenMatched = false;
                    _scan.databaseName = Request["newDatabaseName"]!=null?Request["newDatabaseName"].ToString():string.Empty;
                    _scan.dbId = Convert.ToInt32(dbId);
                    _scan.duplicateScanAllowed = false;
                    _scan.userId = _objUser.UserId;
                    _scan.hashCode = _objUser.HashCode;
                    _scan.iconURL = _databaseImage;
                    _scan.IsInvalidResForced = false;
                    _scan.pattern = string.Empty;
                    _scan.patterValidation = false;
                    _scan.response = string.Empty;
                    _scan.serviceDes = string.Empty;
                    _scan.serviceType = _databaseServiceType;
                    _scan.serviceURL = _databaseServiceURL;
                    msg[0] = _Service.CreateValidateScanService(_scan);

                    break;
                default:
                    break;
            }
            if (msg!=null && msg[0]!=null && Int32.TryParse(msg[0],out serviceId))
            {
                Response.Redirect("ServiceName.aspx?ServiceId="+serviceId.ToString(), false); 
            }
           
        }
        catch (Exception ex)
        {
            Response.Write("Error while creating Database " + ex.Message);
        }
    }
}